<template>
  <div class="compare-page">
    <header class="page-header">
      <div class="container">
        <h1>⚡ 首屏优化性能对比</h1>
        <p>直观感受优化前后的巨大差异</p>
      </div>
    </header>

    <div class="container">
      <!-- 快速对比 -->
      <section class="quick-compare">
        <div class="compare-cards">
          <div class="compare-card slow">
            <div class="card-header">
              <span class="icon">🐌</span>
              <h2>未优化版本</h2>
            </div>
            <div class="card-body">
              <div class="metric">
                <span class="label">首屏时间</span>
                <span class="value bad">~2-3秒</span>
              </div>
              <div class="metric">
                <span class="label">图片体积</span>
                <span class="value bad">~5-10MB</span>
              </div>
              <div class="metric">
                <span class="label">用户体验</span>
                <span class="value bad">⭐⭐</span>
              </div>
            </div>
            <NuxtLink to="/shop-slow" class="test-btn slow-btn">
              🧪 测试未优化版本
            </NuxtLink>
          </div>

          <div class="vs">VS</div>

          <div class="compare-card fast">
            <div class="card-header">
              <span class="icon">🚀</span>
              <h2>优化版本</h2>
            </div>
            <div class="card-body">
              <div class="metric">
                <span class="label">首屏时间</span>
                <span class="value good">~300-500ms</span>
              </div>
              <div class="metric">
                <span class="label">图片体积</span>
                <span class="value good">~500KB-1MB</span>
              </div>
              <div class="metric">
                <span class="label">用户体验</span>
                <span class="value good">⭐⭐⭐⭐⭐</span>
              </div>
            </div>
            <NuxtLink to="/shop-fast" class="test-btn fast-btn">
              ⚡ 测试优化版本
            </NuxtLink>
          </div>
        </div>
      </section>

      <!-- 优化技术详解 -->
      <section class="optimizations">
        <h2 class="section-title">🎯 首屏优化技术详解</h2>
        
        <div class="optimization-grid">
          <!-- 优化1 -->
          <div class="optimization-card">
            <div class="opt-header">
              <span class="opt-number">1</span>
              <h3>SSR 服务端渲染</h3>
            </div>
            <div class="opt-content">
              <p><strong>问题：</strong>客户端渲染需要等待 JS 下载、解析、执行后才能获取数据</p>
              <p><strong>解决：</strong>在服务端就获取数据并渲染 HTML，浏览器直接显示</p>
              <div class="code-example">
                <p class="code-title">❌ 未优化（客户端）：</p>
                <pre>onMounted(async () => {
  const data = await $fetch('/api/products')
  products.value = data
})</pre>
              </div>
              <div class="code-example">
                <p class="code-title">✅ 优化（服务端）：</p>
                <pre>// 在服务端就执行
const { data } = await useFetch('/api/products')</pre>
              </div>
              <div class="benefit">
                <strong>收益：</strong>首屏时间减少 1-2 秒
              </div>
            </div>
          </div>

          <!-- 优化2 -->
          <div class="optimization-card">
            <div class="opt-header">
              <span class="opt-number">2</span>
              <h3>并行数据请求</h3>
            </div>
            <div class="opt-content">
              <p><strong>问题：</strong>串行请求浪费时间，每个请求都要等待上一个完成</p>
              <p><strong>解决：</strong>使用 Promise.all 并行请求所有数据</p>
              <div class="code-example">
                <p class="code-title">❌ 未优化（串行）：</p>
                <pre>const banners = await $fetch('/api/banners')
const products = await $fetch('/api/products')
// 总耗时 = 请求1耗时 + 请求2耗时</pre>
              </div>
              <div class="code-example">
                <p class="code-title">✅ 优化（并行）：</p>
                <pre>const [banners, products] = await Promise.all([
  useFetch('/api/banners'),
  useFetch('/api/products')
])
// 总耗时 = Max(请求1耗时, 请求2耗时)</pre>
              </div>
              <div class="benefit">
                <strong>收益：</strong>数据获取时间减少 50%
              </div>
            </div>
          </div>

          <!-- 优化3 -->
          <div class="optimization-card">
            <div class="opt-header">
              <span class="opt-number">3</span>
              <h3>NuxtImg 图片优化</h3>
            </div>
            <div class="opt-content">
              <p><strong>问题：</strong>加载原始大图片，浪费带宽，加载慢</p>
              <p><strong>解决：</strong>自动压缩、转换格式、调整尺寸</p>
              <div class="code-example">
                <p class="code-title">❌ 未优化：</p>
                <pre>&lt;img src="/image.jpg"&gt;
// 可能加载 2MB 的原图</pre>
              </div>
              <div class="code-example">
                <p class="code-title">✅ 优化：</p>
                <pre>&lt;NuxtImg src="/image.jpg" width="300"&gt;
// 自动优化为 50KB 的 WebP</pre>
              </div>
              <div class="benefit">
                <strong>收益：</strong>图片体积减少 90%，加载速度提升 10 倍
              </div>
            </div>
          </div>

          <!-- 优化4 -->
          <div class="optimization-card">
            <div class="opt-header">
              <span class="opt-number">4</span>
              <h3>首屏优先渲染</h3>
            </div>
            <div class="opt-content">
              <p><strong>问题：</strong>一次性渲染所有内容，阻塞首屏显示</p>
              <p><strong>解决：</strong>只渲染首屏可见内容，其余懒加载</p>
              <div class="code-example">
                <p class="code-title">❌ 未优化：</p>
                <pre>// 一次性渲染所有商品
&lt;ProductCard 
  v-for="product in allProducts"
  :product="product"
/&gt;</pre>
              </div>
              <div class="code-example">
                <p class="code-title">✅ 优化：</p>
                <pre>// 首屏4个立即渲染
&lt;ProductCard v-for="p in firstScreen" /&gt;
// 其余懒加载
&lt;ClientOnly&gt;
  &lt;ProductCard v-for="p in others" /&gt;
&lt;/ClientOnly&gt;</pre>
              </div>
              <div class="benefit">
                <strong>收益：</strong>首屏渲染时间减少 40%
              </div>
            </div>
          </div>

          <!-- 优化5 -->
          <div class="optimization-card">
            <div class="opt-header">
              <span class="opt-number">5</span>
              <h3>骨架屏加载</h3>
            </div>
            <div class="opt-content">
              <p><strong>问题：</strong>数据加载时显示空白，用户焦虑</p>
              <p><strong>解决：</strong>显示骨架屏，给用户即时反馈</p>
              <div class="code-example">
                <p class="code-title">❌ 未优化：</p>
                <pre>&lt;div v-if="loading"&gt;加载中...&lt;/div&gt;
&lt;div v-else&gt;内容&lt;/div&gt;</pre>
              </div>
              <div class="code-example">
                <p class="code-title">✅ 优化：</p>
                <pre>&lt;ProductSkeleton v-if="loading" /&gt;
&lt;ProductCard v-else :product="data" /&gt;</pre>
              </div>
              <div class="benefit">
                <strong>收益：</strong>用户感知的等待时间减少 30%
              </div>
            </div>
          </div>

          <!-- 优化6 -->
          <div class="optimization-card">
            <div class="opt-header">
              <span class="opt-number">6</span>
              <h3>图片懒加载</h3>
            </div>
            <div class="opt-content">
              <p><strong>问题：</strong>加载所有图片，包括屏幕外的</p>
              <p><strong>解决：</strong>只加载可见区域的图片</p>
              <div class="code-example">
                <p class="code-title">❌ 未优化：</p>
                <pre>&lt;img src="/image.jpg"&gt;
// 立即加载所有图片</pre>
              </div>
              <div class="code-example">
                <p class="code-title">✅ 优化：</p>
                <pre>&lt;NuxtImg 
  src="/image.jpg" 
  loading="lazy"
/&gt;</pre>
              </div>
              <div class="benefit">
                <strong>收益：</strong>首屏网络请求减少 70%
              </div>
            </div>
          </div>
        </div>
      </section>

      <!-- 测试建议 -->
      <section class="test-guide">
        <h2 class="section-title">🧪 如何测试对比</h2>
        <div class="guide-steps">
          <div class="step">
            <div class="step-number">1</div>
            <div class="step-content">
              <h3>打开浏览器开发者工具</h3>
              <p>按 F12，切换到 Network 面板</p>
            </div>
          </div>
          <div class="step">
            <div class="step-number">2</div>
            <div class="step-content">
              <h3>模拟慢速网络</h3>
              <p>在 Network 面板选择 "Slow 3G" 或 "Fast 3G"</p>
            </div>
          </div>
          <div class="step">
            <div class="step-number">3</div>
            <div class="step-content">
              <h3>分别测试两个版本</h3>
              <p>先访问未优化版本，再访问优化版本，感受差异</p>
            </div>
          </div>
          <div class="step">
            <div class="step-number">4</div>
            <div class="step-content">
              <h3>观察关键指标</h3>
              <p>查看 Network 的总请求数、传输大小、加载时间</p>
            </div>
          </div>
        </div>
      </section>

      <!-- 性能收益总结 -->
      <section class="benefits-summary">
        <h2 class="section-title">📊 性能收益总结</h2>
        <div class="benefits-grid">
          <div class="benefit-card">
            <div class="benefit-icon">⚡</div>
            <div class="benefit-value">70-80%</div>
            <div class="benefit-label">首屏时间减少</div>
          </div>
          <div class="benefit-card">
            <div class="benefit-icon">📦</div>
            <div class="benefit-value">85-90%</div>
            <div class="benefit-label">传输体积减少</div>
          </div>
          <div class="benefit-card">
            <div class="benefit-icon">🚀</div>
            <div class="benefit-value">5-10倍</div>
            <div class="benefit-label">加载速度提升</div>
          </div>
          <div class="benefit-card">
            <div class="benefit-icon">😊</div>
            <div class="benefit-value">100%</div>
            <div class="benefit-label">用户体验提升</div>
          </div>
        </div>
      </section>
    </div>

    <!-- 底部导航 -->
    <footer class="compare-footer">
      <div class="container">
        <NuxtLink to="/" class="back-btn">⬅️ 返回学习中心</NuxtLink>
      </div>
    </footer>
  </div>
</template>

<script setup lang="ts">
useHead({
  title: '首屏优化性能对比 - Nuxt 4 最佳实践',
  meta: [
    { name: 'description', content: '详细对比首屏优化前后的性能差异，展示 SSR、图片优化、懒加载等技术的实际效果' }
  ]
})
</script>

<style scoped>
.compare-page {
  min-height: 100vh;
  background: #f5f5f5;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 页面头部 */
.page-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 60px 0;
  text-align: center;
}

.page-header h1 {
  font-size: 2.5em;
  margin: 0 0 10px 0;
}

.page-header p {
  font-size: 1.2em;
  margin: 0;
  opacity: 0.9;
}

/* 快速对比区域 */
.quick-compare {
  padding: 60px 0;
}

.compare-cards {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 30px;
  align-items: center;
}

.compare-card {
  background: white;
  border-radius: 15px;
  padding: 30px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.compare-card.slow {
  border: 3px solid #ff6b6b;
}

.compare-card.fast {
  border: 3px solid #4caf50;
}

.card-header {
  text-align: center;
  margin-bottom: 25px;
}

.icon {
  font-size: 48px;
  display: block;
  margin-bottom: 15px;
}

.card-header h2 {
  margin: 0;
  color: #333;
}

.card-body {
  margin-bottom: 25px;
}

.metric {
  display: flex;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.metric:last-child {
  border-bottom: none;
}

.label {
  color: #666;
  font-size: 14px;
}

.value {
  font-weight: bold;
  font-size: 16px;
}

.value.bad {
  color: #ff6b6b;
}

.value.good {
  color: #4caf50;
}

.vs {
  font-size: 2em;
  font-weight: bold;
  color: #999;
}

.test-btn {
  display: block;
  text-align: center;
  padding: 15px;
  border-radius: 8px;
  text-decoration: none;
  color: white;
  font-weight: bold;
  transition: transform 0.3s;
}

.test-btn:hover {
  transform: translateY(-2px);
}

.slow-btn {
  background: linear-gradient(135deg, #ff6b6b, #ee5a6f);
}

.fast-btn {
  background: linear-gradient(135deg, #4caf50, #45a049);
}

/* 优化技术区域 */
.optimizations {
  padding: 60px 0;
}

.section-title {
  font-size: 2em;
  color: #333;
  text-align: center;
  margin-bottom: 40px;
}

.optimization-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(500px, 1fr));
  gap: 30px;
}

.optimization-card {
  background: white;
  border-radius: 10px;
  padding: 25px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.opt-header {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 20px;
}

.opt-number {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  flex-shrink: 0;
}

.opt-header h3 {
  margin: 0;
  color: #333;
}

.opt-content p {
  margin: 10px 0;
  line-height: 1.6;
  color: #666;
}

.code-example {
  margin: 15px 0;
}

.code-title {
  font-size: 13px;
  font-weight: bold;
  margin-bottom: 5px;
  color: #666;
}

pre {
  background: #2d2d2d;
  color: #f8f8f2;
  padding: 15px;
  border-radius: 5px;
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.5;
  margin: 5px 0;
}

.benefit {
  background: #e8f5e9;
  padding: 12px;
  border-radius: 5px;
  margin-top: 15px;
  color: #2e7d32;
  font-size: 14px;
}

/* 测试指南 */
.test-guide {
  padding: 60px 0;
}

.guide-steps {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.step {
  background: white;
  border-radius: 10px;
  padding: 25px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  display: flex;
  gap: 20px;
}

.step-number {
  width: 40px;
  height: 40px;
  background: #667eea;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 18px;
  flex-shrink: 0;
}

.step-content h3 {
  margin: 0 0 10px 0;
  color: #333;
  font-size: 16px;
}

.step-content p {
  margin: 0;
  color: #666;
  font-size: 14px;
  line-height: 1.5;
}

/* 收益总结 */
.benefits-summary {
  padding: 60px 0;
}

.benefits-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

.benefit-card {
  background: white;
  border-radius: 10px;
  padding: 30px;
  text-align: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.benefit-icon {
  font-size: 48px;
  margin-bottom: 15px;
}

.benefit-value {
  font-size: 2.5em;
  font-weight: bold;
  color: #667eea;
  margin-bottom: 10px;
}

.benefit-label {
  color: #666;
  font-size: 14px;
}

/* 底部 */
.compare-footer {
  background: #333;
  padding: 30px 0;
  text-align: center;
}

.back-btn {
  display: inline-block;
  padding: 12px 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  text-decoration: none;
  border-radius: 5px;
  font-weight: bold;
  transition: transform 0.3s;
}

.back-btn:hover {
  transform: translateY(-2px);
}

@media (max-width: 768px) {
  .compare-cards {
    grid-template-columns: 1fr;
  }
  
  .vs {
    transform: rotate(90deg);
  }
  
  .optimization-grid {
    grid-template-columns: 1fr;
  }
}
</style>

